package com.imu.purse.modules.trade.service.impl;

import java.util.Date;
import java.util.List;

import com.imu.purse.modules.app.dao.ImuUserMapper;
import com.imu.purse.modules.app.entity.ImuUserEntity;
import com.imu.purse.modules.bitcoin.form.YtfcoinForm;
import com.imu.purse.modules.trade.service.FundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.trade.dao.YtfTransInMapper;
import com.imu.purse.modules.trade.entity.YtfTransInEntity;
import com.imu.purse.modules.trade.service.YtfTransInService;

/**
 * IMU以太坊原生币转入表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-07-09 19:42:44
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class YtfTransInServiceImpl extends BaseService<YtfTransInEntity> implements YtfTransInService 
{
	@Override
    @Transactional
    public int save(final YtfTransInEntity ytfTransIn)
	{
		ytfTransIn.setCreateTime(System.currentTimeMillis());
    	return super.insert(ytfTransIn);
	}
	
	@Override
	@Transactional
    public int update(final YtfTransInEntity ytfTransIn) 
    {
    	return mapper.updateByPrimaryKeySelective(ytfTransIn);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final YtfTransInEntity ytfTransIn)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(ytfTransIn)))
    	{
        	len = this.insert(ytfTransIn);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    public YtfTransInEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<YtfTransInEntity> queryList(final YtfTransInEntity ytfTransIn)
    {
    	if (ytfTransIn.getPage() != null && ytfTransIn.getLimit() != null)
    	{
            PageHelper.startPage(ytfTransIn.getPage(), ytfTransIn.getLimit());
        }
    	
    	return mapper.findByPage(ytfTransIn);
    }

	/**
	 * B-->A1转入以太坊
	 * @param form
	 * @return
	 */
	@Transactional
	public int ytfTransInData(YtfcoinForm form){
		//
		YtfTransInEntity entity = new YtfTransInEntity();
		entity.setTransId(form.getTransId());
		entity = mapper.selectOne(entity);
		if(entity == null){
			entity = new YtfTransInEntity();
			entity.setTransId(form.getTransId());
			entity.setCreateTime(System.currentTimeMillis());
			entity.setPayAddr(form.getPayAddr());
			entity.setRecAddr(form.getRecAddr());
			entity.setRecUserId(form.getUserId());
			entity.setMoney(form.getMoney());
			ImuUserEntity userEntity = imuUserMapper.selectByPrimaryKey(form.getUserId());
			if(userEntity != null){
				entity.setRecPhone(userEntity.getPhone());
			}
			mapper.insert(entity);
		}
		return 1;
	}
    
	@Autowired
	private YtfTransInMapper mapper;

	@Autowired
	private FundService fundService;

	@Autowired
	private ImuUserMapper imuUserMapper;
}
